//
//  CircleScrollView.m
//  ScrollviewCircle
//
//  Created by liangcheng on 16/2/25.
//  Copyright © 2016年 liangcheng. All rights reserved.
//

#define viewWidth self.frame.size.width
#define viewHeight self.frame.size.height
#define scrollTime 2.0

#import "CircleScrollView.h"

@interface CircleScrollView ()<UIScrollViewDelegate>

@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, strong) UIPageControl *pageControl;
@property (nonatomic, assign) NSInteger currentPage;
@property (nonatomic, strong) NSTimer *timer;
@property (nonatomic, strong) UIImageView *leftImageView;
@property (nonatomic, strong) UIImageView *middleImageView;
@property (nonatomic, strong) UIImageView *rightImageView;
@property (nonatomic, strong) UITapGestureRecognizer *tap;

@end

@implementation CircleScrollView

- (instancetype)initWithFrame:(CGRect)frame {
    
    if (self = [super initWithFrame:frame]) {
        
        self.backgroundColor = [UIColor whiteColor];
        [self addSubview:self.scrollView];
        [self addSubview:self.pageControl];
        [self addTimer];
        //添加手势
        _tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap:)];
        [self.scrollView addGestureRecognizer:_tap];
        
        
    }
    
    return self;
}

- (void)dealloc {
    
    [_timer invalidate];
}

#pragma mark - Private Methods

//点击手势
- (void)handleTap:(UITapGestureRecognizer *)sender {
    
    if ([self.delegate respondsToSelector:@selector(didClickPage:atIndex:)]) {
        [_delegate didClickPage:self atIndex:_currentPage];
    }
}

- (void)removeSubImageViewFromScrollView:(UIScrollView *) scrollView {
    for (UIView *subView in scrollView.subviews) {
        
        if ([subView isKindOfClass:[UIImageView class]]) {
            
            [subView removeFromSuperview];
        }
    }
    
}

//定时器
- (void)addTimer {
    
    self.timer = [NSTimer timerWithTimeInterval:scrollTime target:self selector:@selector(nextPage) userInfo:nil repeats:YES];
    [[NSRunLoop currentRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes];
}

- (void)nextPage {
    
    //下一页实现滑动效果
    [self.scrollView setContentOffset:CGPointMake(CGRectGetWidth(self.scrollView.frame) * 2, 0) animated:YES];
}

//停止滑动
- (void)stopTimer {
    
    if (self.timer) {
        
        [self.timer invalidate]; //停止
        self.timer = nil;
    }
}
#pragma mark - getter

- (UIScrollView *)scrollView {
    
    if (!_scrollView) {
        
        _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.height - 30)];
        _scrollView.backgroundColor = [UIColor whiteColor];
        _scrollView.pagingEnabled = YES;
        _scrollView.showsHorizontalScrollIndicator = NO;
        _scrollView.delegate = self;
    }
    
    return _scrollView;
}

- (UIPageControl *)pageControl {
    
    if (!_pageControl) {
        
        _pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(viewWidth - 130, viewHeight - 30, 100, 30)];
        _pageControl.pageIndicatorTintColor = [UIColor grayColor];
        _pageControl.currentPageIndicatorTintColor = [UIColor blackColor];
        
    }
    
    return _pageControl;
}

- (UIImageView *)leftImageView {
    
    if (!_leftImageView) {
        
        _leftImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, viewWidth, viewHeight - 30)];
    }
    
    return _leftImageView;
}
- (UIImageView *)middleImageView {
    
    if (!_middleImageView) {
        
        _middleImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, viewWidth, viewHeight - 30)];
    }
    
    return _middleImageView;
}

- (UIImageView *)rightImageView {
    
    if (!_rightImageView) {
        
        _rightImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, viewWidth, viewHeight - 30)];
    }
    
    return _rightImageView;
}


#pragma mark - setter

- (void)setImageDataSource:(NSArray *)imageDataSource {
    
    _imageDataSource = imageDataSource;
    
    [self removeSubImageViewFromScrollView:self.scrollView];
    if (imageDataSource.count > 1) {
        
        _leftImageView.frame = CGRectMake(0, 0, viewWidth, viewHeight - 30);
        self.leftImageView.image = [UIImage imageNamed:[imageDataSource lastObject]];
        [self.scrollView addSubview:self.leftImageView];
        
        self.middleImageView.frame = CGRectMake(viewWidth, 0, viewWidth, viewHeight - 30);
        self.middleImageView.image = [UIImage imageNamed:imageDataSource[0]];
        [self.scrollView addSubview:self.middleImageView];
        
        self.rightImageView.frame = CGRectMake(viewWidth * 2, 0, viewWidth, viewHeight - 30);
        self.rightImageView.image = [UIImage imageNamed:imageDataSource[1]];
        [self.scrollView addSubview:self.rightImageView];
        
        self.scrollView.contentSize = CGSizeMake(viewWidth * 3, viewHeight - 30);
        //设置偏移量
        [self.scrollView setContentOffset:CGPointMake(viewWidth, 0) animated:YES];
        self.pageControl.numberOfPages = imageDataSource.count;
        self.pageControl.currentPage = 0;
    }
}

#pragma mark - UIScrollViewDelegate

//开始拖动图片的时候停止自动轮播
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
    
    [self stopTimer];
}

//当视图停止拖动的时候
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(scrollTime * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        
        [self addTimer];
    });
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    
    NSInteger index = scrollView.contentOffset.x / viewWidth;
    if (index == 2) { //如果滑动到最后一张图片
        
        _currentPage = _currentPage + 1;
        if (_currentPage == _imageDataSource.count) {
            
            _currentPage = 0;
            self.leftImageView.image = [UIImage imageNamed:[_imageDataSource lastObject]];
            self.middleImageView.image = [UIImage imageNamed:_imageDataSource[_currentPage]];
            self.rightImageView.image = [UIImage imageNamed:_imageDataSource[_currentPage + 1]];
        }else {
            
            self.leftImageView.image = [UIImage imageNamed:_imageDataSource[_currentPage - 1]];
            self.middleImageView.image = [UIImage imageNamed:_imageDataSource[_currentPage]];
            self.rightImageView.image = [UIImage imageNamed:_currentPage + 1 == _imageDataSource.count ? _imageDataSource[0] : _imageDataSource[_currentPage + 1]];
        }
    }else if (index == 0) {
        
        _currentPage = _currentPage - 1;
        if (_currentPage == -1) {
            
            _currentPage = _imageDataSource.count - 1;
            self.leftImageView.image = [UIImage imageNamed:_imageDataSource[_currentPage - 1]];
            self.middleImageView.image = [UIImage imageNamed:_imageDataSource[_currentPage]];
            self.rightImageView.image = [UIImage imageNamed:_imageDataSource[0]];
        }else {
            
            self.leftImageView.image = [UIImage imageNamed:_currentPage - 1 < 0 ? _imageDataSource[_imageDataSource.count - 1] : _imageDataSource[_currentPage - 1]];
            self.middleImageView.image = [UIImage imageNamed:_imageDataSource[_currentPage]];
            self.rightImageView.image = [UIImage imageNamed:_imageDataSource[_currentPage + 1]];
        }
    }
    
    [self.scrollView setContentOffset:CGPointMake(CGRectGetWidth(self.frame), 0) animated:NO];
    self.pageControl.currentPage = _currentPage;
    
}

- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView {
    NSInteger index = scrollView.contentOffset.x / CGRectGetWidth(self.frame);
    if (index == 2) {
        
        _currentPage = _currentPage + 1;
        if (_currentPage == _imageDataSource.count) {
            _currentPage = 0;
            self.leftImageView.image = [UIImage imageNamed:[_imageDataSource lastObject]];
            self.middleImageView.image = [UIImage imageNamed:_imageDataSource[_currentPage]];
            self.rightImageView.image = [UIImage imageNamed:_imageDataSource[_currentPage + 1]];
            
        }else {
            
            self.leftImageView.image = [UIImage imageNamed:_imageDataSource[_currentPage - 1]];
            self.middleImageView.image = [UIImage imageNamed:_imageDataSource[_currentPage]];
            self.rightImageView.image = [UIImage imageNamed:_currentPage + 1 == _imageDataSource.count ? _imageDataSource[0] : _imageDataSource[_currentPage + 1]];
        }
    }else if (index == 0) {
        
        _currentPage = _currentPage - 1;
        if (_currentPage == -1) {
            _currentPage = _imageDataSource.count - 1;
            self.leftImageView.image = [UIImage imageNamed:_imageDataSource[_currentPage - 1]];
            self.middleImageView.image = [UIImage imageNamed:_imageDataSource[_currentPage]];
            self.rightImageView.image = [UIImage imageNamed:_imageDataSource[0]];
        }else {
            self.leftImageView.image = [UIImage imageNamed:_currentPage - 1 < 0 ? _imageDataSource[_imageDataSource.count - 1] : _imageDataSource[_currentPage - 1]];
            self.middleImageView.image = [UIImage imageNamed:_imageDataSource[_currentPage]];
            self.rightImageView.image = [UIImage imageNamed:_imageDataSource[_currentPage + 1]];
        }
    }
    [self.scrollView setContentOffset:CGPointMake(CGRectGetWidth(self.frame), 0) animated:NO];
    self.pageControl.currentPage = _currentPage;
}


@end
